﻿using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ECP.Service.Utilities;
using ECP.Model;
using ECP.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client
{
    public partial class FormBase : DevExpress.XtraEditors.XtraForm
    {
        public FormBase()
        {
            InitializeComponent();
        }

        public bool Busyness;
        public bool Changed;
        public bool ExitApplication;
        public bool FormLoaded;
        private BaseUserInfo userInfo = new BaseUserInfo();

        private void FormECP_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
                this.GetIcon();
                this.FormOnLoad();
            }
            catch (Exception exception)
            {
                BaseInterfaceLogic.WriteException(this.UserInfo, exception);
                new FrmException(this.UserInfo, exception).ShowDialog();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void FormECP_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.FormOnClosed();
            }
            catch (Exception exception)
            {
                BaseInterfaceLogic.WriteException(this.UserInfo, exception);
                new FrmException(this.UserInfo, exception).ShowDialog();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        public void ExportExcel(ECP.Utilities.Controls.ECPGridControl dataGridView, string directory, string fileName)
        {
            this.Cursor = Cursors.WaitCursor;
            string path = BaseSystemInfo.StartupPath + directory;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string str2 = BaseSystemInfo.StartupPath + directory + fileName;
            if (!this.FileExist(str2))
            {
                //    BaseExportCSV.ExportCSV(dataGridView, str2);
                Process.Start(str2);
            }
            this.Cursor = Cursors.Default;
        }

        private bool FileExist(string fileName)
        {
            if (File.Exists(fileName))
            {
                string str = Path.GetFileName(fileName);
                if (MessageBox.Show("导出的目标文件已存在要覆盖 \"" + str + "\" 吗？", AppMessage.MSG0000, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
                {
                    return true;
                }
                File.Delete(fileName);
            }
            return false;
        }

        private void FormOnClosed()
        {
            if (BaseSystemInfo.RecordLog)
            {
                ServiceManager.Instance.LogService.WriteLog(this.UserInfo, base.Name, this.Text, "FormClosed");
            }
        }

        private void FormOnLoad()
        {
            if (BaseSystemInfo.RecordLog)
            {
                ServiceManager.Instance.LogService.WriteLog(this.UserInfo, base.Name, this.Text, "FormLoad");
            }
        }

        public virtual void GetIcon()
        {
            if (File.Exists(BaseSystemInfo.FormIco))
            {
                base.Icon = new Icon(BaseSystemInfo.FormIco);
            }
        }

        public DataTable GetOrganizeScope(string permissionCode)
        {
            if (!this.UserInfo.IsAdministrator && !string.IsNullOrEmpty(permissionCode))
            {
                return ServiceManager.Instance.PermissionService.GetOrganizeDTByPermission(this.UserInfo, this.UserInfo.ID, permissionCode);
            }
            DataTable table = ClientCache.Instance.GetOrganizeList(this.UserInfo).Copy();
            table.DefaultView.Sort = BaseOrganizeTable.FieldSortCode;
            return table;
        }

        public DataTable GetPermissionLicensed()
        {
            if (this.UserInfo.IsAdministrator)
            {
                return ServiceManager.Instance.PermissionAdminService.GetDT(this.UserInfo);
            }
            return ServiceManager.Instance.PermissionService.GetLicensePermissionByUser(this.UserInfo, this.UserInfo.ID);
        }

        public DataTable GetUserScope(string permissionCode)
        {
            if (!this.UserInfo.IsAdministrator && !string.IsNullOrEmpty(permissionCode))
            {
                return ServiceManager.Instance.PermissionService.GetUserDTByPermission(this.UserInfo, this.UserInfo.ID, permissionCode);
            }
            return ServiceManager.Instance.UserService.GetDT(this.UserInfo);
        }

        public bool IsAuthorized(string permissionCode)
        {
            if (ClientCache.Instance.DTPermission == null)
            {
                return false;
            }
            if (this.UserInfo.IsAdministrator)
            {
                return true;
            }

            return BaseBusinessLogic.IsAuthorized(ClientCache.Instance.DTPermission, this.UserInfo, permissionCode);
        }

        public void Localization(Form form)
        {
            BaseInterfaceLogic.SetLanguageResource(form);
        }

        public void ProcessException(Exception exception)
        {
            this.WriteException(exception);
            new FrmException(this.UserInfo, exception).ShowDialog();
        }

        public void WriteException(Exception exception)
        {
            BaseInterfaceLogic.WriteException(this.UserInfo, exception);
        }

        public BaseUserInfo UserInfo
        {
            get
            {
                this.userInfo.GetUserInfo();
                return this.userInfo;
            }
            set
            {
                this.userInfo = value;
            }
        }

        public delegate void SetControlStateEventHandler(bool setTop, bool setUp, bool setDown, bool setBottom, bool add, bool edit, bool batchDelete, bool batchSave);

        public bool Authorization(string permissionCode)
        {
            return this.IsAuthorized(permissionCode);
        }

        public virtual DisplayTypes DisplayType
        {
            get;
            set;
        }

        public virtual string ModelCode
        {
            get { return this.GetType().FullName; }
        }

        public virtual string ModelName
        {
            get { return this.Text; }
        }

        public virtual void StartModel()
        {
            base.ShowDialog();
        }

      
    }
}